package com.xsprite.managesys.listener;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.comet4j.core.CometConnection;
import org.comet4j.core.CometContext;
import org.comet4j.core.CometEngine;
import org.comet4j.core.event.ConnectEvent;
import org.comet4j.core.event.DropEvent;
import org.comet4j.core.listener.ConnectListener;
import org.comet4j.core.listener.DropListener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xsprite.managesys.bean.Message;
import com.xsprite.managesys.log.LogManager;
import com.xsprite.managesys.service.MessageManager;

public class CommunicationListener 
//	implements ServletContextListener 
	{

//	private static final String CHANNEL = "workBench";
//	
//
//	public void contextDestroyed(ServletContextEvent arg0) {
//		connsMap.clear();
//	}
//
//	private Map<String,CopyOnWriteArrayList<CometConnection>> connsMap = new HashMap<String, CopyOnWriteArrayList<CometConnection>>();
//	
//	public void contextInitialized(ServletContextEvent arg0) {
//		CometContext cc = CometContext.getInstance();
//		CometContext.getInstance().getEngine().addConnectListener(new JoinListener());
//		CometContext.getInstance().getEngine().addDropListener(new OutListener());
//		cc.registChannel(CHANNEL);// 注册应用的channel
//		Thread helloAppModule = new Thread(new HelloAppModule(), "Sender App Module");
//		helloAppModule.setDaemon(true);
//		helloAppModule.start();
//	}
//
//	class JoinListener extends ConnectListener {
//        @Override
//        public boolean handleEvent(ConnectEvent anEvent) {
//                CometConnection conn = anEvent.getConn();
//                String userid = conn.getRequest().getSession().getAttribute("userid").toString();
//                LogManager.getInstance().logAll("控制台连接成功", userid);
//                CopyOnWriteArrayList<CometConnection> conns = connsMap.get(userid);
//                if(conns == null){
//                	CopyOnWriteArrayList<CometConnection> tempConnsList = new CopyOnWriteArrayList<CometConnection>();
//                	tempConnsList.add(conn);
//                	connsMap.put(userid, tempConnsList);
//                } else {
//                	conns.add(conn);
//                }
//                return true;
//        }
//	}
//	
//	class OutListener extends DropListener{
//
//		@Override
//		public boolean handleEvent(DropEvent event) {
//			String userid = event.getConn().getRequest().getSession().getAttribute("userid").toString();
//			List<CometConnection> conns = connsMap.get(userid);
//			if(conns.contains(event.getConn())){
//				conns.remove(event.getConn());
//			}
//			LogManager.getInstance().logAll("控制台断开连接", userid + "left conn : " + conns.size());
//			return true;
//		}
//	}
//	
//	
//	
//	class HelloAppModule implements Runnable {
//		CometEngine engine = CometContext.getInstance().getEngine();
//		public void run() {
////			long lasttime = 0;
//			while (true) {
////				long now = System.currentTimeMillis();
////				if((now - lasttime) <= 50){
////					continue;
////				}
////				lasttime = now;
//				Message msg = MessageManager.getInstance().pollMessage();
//				if(msg != null){
//					JSONObject msgJSON = (JSONObject) JSON.toJSON(msg);
//					List<CometConnection> conns = connsMap.get(msg.getUid());
//					if(conns == null || conns.size() == 0){
//						LogManager.getInstance().log4j("userid=" + msg.getUid() + "	找不到对话:");
//						continue;
//					}
//					Iterator<CometConnection> connItr = conns.iterator();
//					while(connItr.hasNext()){
//						engine.sendTo(CHANNEL, connItr.next(), msgJSON.toJSONString());
//					}
//				}
//			}
//		}
//	}
}
